<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<div id="unspport" style="padding:40px;display:none;">
	<div>
		<h2>无法执行读卡操作</h2>

		<p>1. 命令已经被用户禁止！</p>

		<p>
			2. 未安装插件！点<a href="${pageContext.request.contextPath}/Locked.zip">这里</a>下载安装,安装方法见压缩包内的说明文件。
		</p>

		<p>2. 此功能必须使用ie浏览器，其他浏览器不支持！</p>
	</div>
</div>
<div id="read_card" style="text-align:center;vertical-align: middle;padding:80px;display:none;">
	<div>
		<img src="${pageContext.request.contextPath}/images/loading.gif">
		<br> <br>

		<p>等待读卡中，请确保连接成功读卡器，并将其串口号改为9，将智能卡放入读卡器内</p>

		<div style="padding: 5px;text-align: center;">
			<a href="javascript:;" class="easyui-linkbutton"
				onclick="ReadIDCard()" data-options="iconCls:'icon-save'">开始读卡</a>
		</div>
	</div>
</div>
<div id="write_card" style="display:none;">
	<form id="write_card_form" name="write_card_form" method="post">
		<table class="listtable" style="width: 100%;">
			<tr class="row-even">
				<td class="tr_left"><span class="required">*</span>智能卡号：</td>
				<td class="tr_right"><input type="text" name="Cardsn"
					maxlength="200" class="easyui-validatebox" style="width: 300"
					data-options="required:true,missingMessage:'请输入智能卡号'"
					readonly="readonly" /></td>
			</tr>
			<tr style="display: none">
				<td class="tr_left"><span class="required">*</span>门锁号：</td>
				<td class="tr_right"><textarea name="RoomNo" maxlength="200"
						class="easyui-validatebox" rows="4" style="width: 300"
						data-options="required:true,missingMessage:'请输入门锁号'"
						readonly="readonly">${doorNos}</textarea></td>
			</tr>

			<tr style="display: none">
				<td class="tr_left"><span class="required">*</span>人员id：</td>
				<td class="tr_right"><input name="empId" maxlength="200"
					value="${empId}" class="easyui-validatebox" rows="4"
					style="width: 300"
					data-options="required:true,missingMessage:'请确认人员'"
					readonly="readonly" /></td>
			</tr>

			<tr class="row-even">
				<td class="tr_left"><span class="required">*</span>入住时间：</td>
				<td class="tr_right"><input id="InTime" name="InTime"
					maxlength="200" value="${stayTime}" class="easyui-validatebox"
					rows="4" style="width: 300"
					data-options="required:true,missingMessage:'请输入入住时间'"
					readonly="readonly" /></td>
			</tr>
			<tr>
				<td class="tr_left"><span class="required">*</span>预离时间：</td>
				<td class="tr_right"><input id="OutTime" name="OutTime"
					maxlength="200" value="${outTime}" class="easyui-validatebox"
					rows="4" style="width: 300"
					data-options="required:true,missingMessage:'请输入入住时间'"
					readonly="readonly" /></td>
			</tr>
			<tr>
				<td class="tr_left"><span class="required">*</span>终止旧卡：</td>
				<td class="tr_right"><input type="checkbox" id="radio" /></td>
			</tr>
		</table>
		<div style="padding: 5px;text-align: center;">
			<a href="javascript:;" class="easyui-linkbutton" onclick="MakeCard()"
				data-options="iconCls:'icon-save'">确认无误，开始写卡</a>
		</div>
	</form>
</div>
<object id="csharpActiveX"
	classid="clsid:FFD3E742-47CD-4E67-9613-1BB0D67554FF"
	style="display: none;"></object>
<script type="text/javascript">
	
	var activeX = document.getElementById("csharpActiveX");

	var Com = 9, CardNo = 1, nBlock = 4, Encrypt = 1, CardPass = "82A094FFFFFF",
	 SystemCode = "0A7E2FED", HotelCode = "1", Pass,  SDIn, STIn, SDOut, 
	 STOut, LEVEL_Pass=3, PassMode, AddressMode = 0, AddressQty = 1, 
	 TimeMode = 0, V8 = 255, V16 = 255, V24 = 255, AlwaysOpen = 0, OpenBolt = 0, 
	 TerminateOld = 0, ValidTimes = 255;

	

	

	function ReadIDCard() {
	var activeReadCardSN = activeX.ReadCardSN(Com);

	//	var cardsn = activeX.ReadCardSN_CardSN;
 var readBlock=activeX.ReadBlock(9,0,0,"FFFFFFFFFFFF");
    var readRecardData=activeX.ReadRecordData();
    var cardsn=readRecardData.substring(0,8).toUpperCase();
    
		$('input[name="Cardsn"]').val(cardsn);
        $('#read_card').hide();
		$('#write_card').show();
		
	}

	
function HideActiveX() {
        if (!!window.ActiveXObject || "ActiveXObject" in window) {
           // ClearIDCard();
           // ClearForm();
            $('#read_card').show();
            return true;
        } else {
            $('#unspport').show();
            return;
        }
    }
    HideActiveX();
	
	
	function MakeCard() {
	 var Address = $('textarea[name="RoomNo"]').val();
	 
	 var locked = "${locked}";
	 
	// alert(locked);
if (document.getElementById("radio").checked) {
			PassMode = 2;
			Pass = activeX.CompactCipherTime();
		//	alert(Pass+"!!1");
			$
					.ajax({
						url : '${pageContext.request.contextPath}/cnbdw/roomAction!updateRoomLocked.action',
						dataType : 'json',
						data : {
							doorNo : Address,
							lock : Pass
						},
						success : function(result) {
						}

					});

		} else {
			if (locked == null||locked=="") {
				Pass = activeX.CompactCipherTime();
				//alert(Pass+"!!2");
				$
						.ajax({
							url : '${pageContext.request.contextPath}/cnbdw/roomAction!updateRoomLocked.action',
							dataType : 'json',
							data : {
								doorNo : Address,
								lock : Pass
							},
							success : function(result) {
							}

						});
			} else {

				Pass = locked;
			//	alert(Pass+"!!3");
			}
			PassMode = 1;
		}
       // Pass="24DAF3";
		var cardsnr = $('input[name="CardNo"]').val();

		var intime = "${stayTime}";
		var outtime = "${outTime}";
		var empId = $('input[name="empId"]').val();

		var str = intime.split(" ");
		var str1 = outtime.split(" ");
		SDIn = str[0];
		STIn = str[1];
		SDOut = str[0];
		STOut = str[1];
		CardNo = CardNo + 1;
//alert(SDIn+"?????");

		var pResult = activeX.KeyCard(Com, CardNo, nBlock, Encrypt, CardPass,
				SystemCode, HotelCode, Pass, Address, SDIn, STIn, SDOut, STOut,
				LEVEL_Pass, PassMode, AddressMode, AddressQty, TimeMode, V8,
				V16, V24, AlwaysOpen, OpenBolt, TerminateOld, ValidTimes);

		$.messager.show({
			title : '温馨提示',
			msg : "写卡结果：" + (pResult == 0 ? "成功" : "失败"),
			timeout : 5000,
			showType : 'slide'
		});

		

	}
	
	
	
	
	
	
</script>